package com.moorwind.chartservice.utils
{
	import com.moorwind.chartservice.data.ChartVO;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	public class NetUtil
	{		
		public static function getData(url:String, param:URLVariables, callback:Function, onError:Function = null):void
		{
			var loader:URLLoader = new URLLoader();
			
			var request:URLRequest = new URLRequest(url);
			request.method = URLRequestMethod.GET;
			request.data = param;
			
			loader.addEventListener(Event.COMPLETE, callback);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		public static function postData(url:String, param:URLVariables, callback:Function, onError:Function = null):void
		{
			var loader:URLLoader = new URLLoader();
			
			var request:URLRequest = new URLRequest(url);
			request.method = URLRequestMethod.POST;
			request.data = param;
			
			loader.addEventListener(Event.COMPLETE, callback);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			loader.load(request);
		}
		
		public static function getChartData(chart_id:int, callback:Function, onError:Function = null):void
		{
			var param:URLVariables = new URLVariables();			
			param.chart_id = chart_id;
			param.timestamp = new Date().getTime();		
			var request:URLRequest = new URLRequest(ChartConfig.instance.CHART_GET_CHART);
			request.method = URLRequestMethod.GET;
			request.data = param;
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, callback);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			
			loader.load(request);
		}
		
		public static function postChartData(chart:ChartVO, key:String, callback:Function, onError:Function = null):void
		{
			var param:URLVariables = chart.toURL();
			param.signature = CryptoUtils.createSinature(key, param);
			
			postData(ChartConfig.instance.CHART_SAVE_CHART, param, callback, onError);
		}
		

	}
}